#!/bin/bash
INPUT=/tmp/menu.sh.$$
OUTPUT=/tmp/output.sh.$$
TMP=/tmp
scr_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

#
# WebServer Section
#

webserver() {

	gitoriousInst() {

			gitx86bit() {
				clear
			    	echo '********************************************************************'
			    	echo '* Installation lastet version of Bitnami Gitorious x86 in progress *'
			    	echo '********************************************************************'
			    	sleep 3
				###x86###
				cd /tmp
				findout=$(find $TMP/bitna*linux-in*.run) && rm $findout
				wget -O $TMP/ https://bitnami.com/stack/gitorious -q \
				| grep 'href=".*\.run"' \
				| sed -e 's/^.*href="\(.*redirect.*.linux-installer.*run\)".*$/\1/' \
				| xargs -i{} wget -O $TMP/ https://bitnami.com{}
				###instalation###
				findout=$(find $TMP/bitna*linux-in*.run) | chmod +x $findout | $findout

			}
			
			gitx64bit() {
				clear
			    	echo '********************************************************************'
			    	echo '* Installation lastet version of Bitnami Gitorious x64 in progress *'
			    	echo '********************************************************************'
			    	sleep 3
				###x64###
				cd /tmp
				findout=$(find bitna*linux-x64-in*.run) && rm $findout
				wget https://bitnami.com/stack/gitorious -q O - \
				| grep 'href=".*\.run"' \
				| sed -e 's/^.*href="\(.*redirect.*.linux-x64-installer.*run\)".*$/\1/' \
				| xargs -i{} wget https://bitnami.com{}
				###instalation###
				findout=$(find bitna*linux-x64-in*.run) | chmod +x $findout | $findout
			}
			
			gitOfficial() {
				clear
			    	echo '*****************************************************************************'
			    	echo '* Installation lastet version of Gitorious from official source in progress *'
			    	echo '*****************************************************************************'
			    	sleep 3
				cd /tmp
				apt-get install -y git
				git clone https://git.gitorious.org/gitorious/ce-installer.git && cd ce-installer
				./install

			}

		### display main menu ###
		dialog --clear  --help-button --backtitle "Universal System Admin Tool v1.0" \
		--title "[ G I T O R I O U S  M A I N  M E N U ]" \
		--menu "You can use the UP/DOWN arrow keys, the first letter of the choice as a hot key, or the number keys 1-9 to choose an option.
		Choose the TASK" 15 90 4 \
		Gitorious_x86  "Install lastet version of Bitnami Gitorious x86" \
		Gitorious_x64 "Install lastet version of Bitnami Gitorious x64" \
		Gitorious_offcial "Install lastet version of Gitorious from official source" \
		Exit "Exit to the shell" 2>"${INPUT}"
		 
		menuitem=$(<"${INPUT}")

		# make decsion 
		case $menuitem in
			Gitorious_x86) gitx86bit;;
			Gitorious_x64) gitxbit;;
			Gitorious_offcial) bar;;
			Exit) echo "Bye"; break;;
		esac
	}
cmd=(dialog --backtitle "Universal System Admin Tool v1.0" --title "[WEBSERVER INSTALLATION MENU]" --separate-output --checklist "Select options:" 15 90 8)
options=(1 "Install Apache 2.4" off    # any option can be set to default to "on"
         2 "Install PHP 5.6" off
         3 "Install PostgreSQL 9.4" off
         4 "Install OpenSSL" off
	 5 "Install OpenSSH Server" off
	 6 "Install Gitorious" off)

choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
clear

for choice in $choices
do
    case $choice in
        1)  
	    clear
	    echo '***********************************'
	    echo '* Apache2 instalation in progress *'
	    echo '***********************************'
	    sleep 3
            apt-get install -y apache2
            ;;
        2)
	    clear
	    echo '***********************************'
	    echo '* PHP 5.6 instalation in progress *'
	    echo '***********************************'
	    sleep 3
            add-apt-repository -y ppa:ondrej/php5-5.6 && apt-get update && apt-get install -y php5 php5-cli 
	while true; do
	    read -p "Do you wish add phpinfo.php file to /var/www/html? " yn
	    case $yn in
	        [Yy]* ) echo '<?php phpinfo();' >> /var/www/html/phpinfo.php
			echo 'Adding phpinfo.php'
			sleep 3
		break;;
	        [Nn]* ) exit;;
	        * ) echo "Please answer yes or no.";;
    esac
done
            ;;
        3)
	    clear
	    echo '****************************************************************'
	    echo '* PostgreSQL 9.4 installation in progress, it may take a while *'
	    echo '****************************************************************'
	    sleep 3
            echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' >> /etc/apt/sources.list.d/pgdg.list && wget https://www.postgresql.org/media/keys/ACCC4CF8.asc && sudo apt-key add ACCC4CF8.asc && apt-get update && apt-get install -y postgresql-9.4
            ;;
        4)
	    clear
	    echo '***********************************'
	    echo '* OpenSSL instalation in progress *'
	    echo '***********************************'
	    sleep 3
            apt-get install -y openssl libssl0.9.8
            ;;
	5)
	    clear
	    echo '******************************************'
	    echo '* OpenSSH Server instalation in progress *'
	    echo '******************************************'
	    sleep 3
	    apt-get install -y openssh-server
	    ;;
	6)
	    gitoriousInst
	    ;;
    esac
done
}

#
# System Administration and Monitoring section
#

sysadm(){

        webmin(){

        clear
	    echo '******************************************'
	    echo '*     Webmin instalation in progress     *'
	    echo '******************************************'
	    sleep 3
        cd /tmp
        wget 
        }
}

#
# Backup and restore section
#

bar() {

	snapshot() {
		snapshot_dir=$(dialog --stdout --title "Where is snapshot.tar" --fselect / 14 48)
			case $? in
			0)
				yesno_snap
				;;
                        1)
				clear
				bar
				;;	 
			esac
	}

	yesno_snap() {
		dialog --stdout --title "Question" --yesno "Are you sure $snapshot_dir?" 14 48
			case $? in
			 0)
				;;
			 1)
				clear
				snapshot
				;;	 
			esac
	}

	igd() {
		gitorious_dir=$(dialog --stdout --title "Where is installed Gitorious?" --fselect / 14 48)
			case $? in
			 0)
				yesno
				;;
		         1)
				clear
				bar
				;;	 
			esac
		
	}

	yesno() {
		dialog --stdout --title "Question" --yesno "Are you sure $gitorious_dir ?" 14 48
			case $? in
				 0)
					;;
				 1)
					clear
					igd
					;;	 
				esac
	}


	snapshot_func() {
		    igd
		    echo '******************************'
		    echo '*     Gitorious Snapshot     *'
		    echo '******************************'
		    echo ''	
		    echo 'Preparing...'		    
		    sleep 3
		    echo ''
		    if [ -e $gitorious_dir/script/snapshot ]; then
			    echo 'Runnig snapshot script'
			    sleep 3
			    chmod +x $gitorious_dir/script/snapshot && $gitorious_dir/script/snapshot
		    else
		      	    echo 'Copying snapshot script'
			    sleep 3
			    cp $scr_dir/gitorious_scripts/snapshot $gitorious_dir/script/ && chmod +x $gitorious_dir/script/snapshot && cp $scr_dir/gitorious_scripts/backup.rake $gitorious_dir/lib/task/ 
			    echo "Running restore script"
	 		    sleep 3
			    $gitorious_dir/script/snapshot
			    
		    fi
	}

	restore_func() {
		    igd
		    snapshot
		    echo '******************************'
		    echo '*      Gitorious Restore      *'
		    echo '******************************'
		    echo ''			    
		    sleep 3
		    echo ''
		    if [ -e $gitorious_dir/script/restore ]; then
			    echo 'Running restore script'
			    sleep 3
			    chmod +x $gitorious_dir/script/restore && $gitorious_dir/script/restore $snapshot_dir
		    else 
			    echo 'Copying restore script'
			cp $scr_dir/gitorious_scripts/restore $gitorious_dir/script/ && chmod +x $gitorious_dir/script/restore && cp $scr_dir/gitorious_scripts/backup.rake $gitorious_dir/lib/task/
			echo "Running restore script"
			sleep 3
			$gitorious_dir/script/restore $snapshot_dir
		    fi
	}

	### display main menu BAR ###
	dialog --clear --backtitle "Universal System Admin Tool v1.0" \
	--title "[GITORIOUS - BACKUP AND RESTORE ]" \
	--menu "You can use the UP/DOWN arrow keys, the first letter of the choice as a hot key, or the number keys 1-9 to choose an option.
	Choose the TASK" 15 90 5 \
	Snapshot  "Make backup current Gitorious db" \
	Restore "Restore Gitorious db from snapshot.tar" \
	Exit "Exit to the shell" 2>"${INPUT}"
	 
	menuitem=$(<"${INPUT}")

	# make decsion bar
	case $menuitem in
		Snapshot) snapshot_func;;
		Restore) restore_func;;
		Exit) echo "Bye"; break;;
	esac

}


while true
do
 
### display main menu ###
dialog --clear  --help-button --backtitle "Universal System Admin Tool v1.0" \
--title "[ M A I N - M E N U ]" \
--menu "You can use the UP/DOWN arrow keys, the first letter of the choice as a hot key, or the number keys 1-9 to choose an option.
Choose the TASK" 15 90 4 \
WebServer  "Install Apache2, PHP 5.6 etc." \
SysAdministration "Install tools for System Administration" \
Backup/Restore "Backup and Restore Gitorious db and another" \
Exit "Exit to the shell" 2>"${INPUT}"
 
menuitem=$(<"${INPUT}")

# make decsion 
case $menuitem in
	WebServer) webserver;;
	SysAdministration) show_calendar;;
	Backup/Restore) bar;;
	Exit) echo "Bye"; break;;
esac
 
done
